package com.ygp.core;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.LongAdder;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class UseConcurrentHashMap {
    static final String ALPHA = "abcedfghijklmnopqrstuvwxyz";

    public static void main(String[] args) {
//        prepareData();
        Supplier<Map<String, LongAdder>> map = ConcurrentHashMap::new;
//        Supplier<Map<String, Integer>> map = ConcurrentHashMap::new;
        count(map, consumer2());

    }

    private static BiConsumer<Map<String, Integer>, List<String>> consumer() {
        return (map, words) -> {
            for (String word : words) {
                Integer count = map.get(word);
                int newValue = count == null ? 1 : count + 1;
                map.put(word, newValue);
            }
        };
    }

    private static BiConsumer<Map<String, LongAdder>, List<String>> consumer2() {
        return (map, words) -> {
            for (String word : words) {
                map.computeIfAbsent(word, (key) -> new LongAdder()).increment();
            }
        };
    }

    private static <V> void count(Supplier<Map<String, V>> supplier,
                                  BiConsumer<Map<String, V>, List<String>> consumer) {
        Map<String, V> counterMap = supplier.get();
        List<Thread> ts = new ArrayList<>();
        for (int i = 1; i <= 26; i++) {
            int idx = i;
            Thread thread = new Thread(() -> {
                List<String> words = readFromFile(idx);
                consumer.accept(counterMap, words);
            });
            ts.add(thread);
        }
        ts.forEach(Thread::start);
        ts.forEach(t -> {
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        System.out.println(counterMap);
    }

    public static List<String> readFromFile(int i) {
        try {
            Path path = new File("E:\\tmpl\\" + i + ".txt").toPath();
            List<String> total = Files.readAllLines(path);
            return total;
        } catch (IOException e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
    }

    private static void prepareData() {
        int length = ALPHA.length();
        int count = 200;
        List<String> list = new ArrayList<>(length * count);
        for (int i = 0; i < length; i++) {
            char ch = ALPHA.charAt(i);
            for (int j = 0; j < count; j++) {
                list.add(String.valueOf(ch));
            }
        }
        Collections.shuffle(list);
        for (int i = 0; i < 26; i++) {
            try (PrintWriter out = new PrintWriter(
                    new OutputStreamWriter(
                            new FileOutputStream("E:\\tmpl\\" + (i + 1) + ".txt")))) {
                String collect = list.subList(i * count, (i + 1) * count).stream()
                        .collect(Collectors.joining("\n"));
                out.print(collect);
            } catch (IOException e) {
            }
        }
    }
}

